import { EventEmitter } from "../event/EventEmitter";
import { IMediator, IValid } from "../interfaces";
import { AppEvent } from "../event/AppEvent";
import { AppEventName } from "../Define";
import { IUIView } from "./IUIView";
import { LayerItem } from "./ui";
import LayerManager from "./LayerManager";

export class UIManager extends EventEmitter implements IValid {

    protected managers: LayerManager[] = []

    constructor(mediator: IMediator, root: any, settings: LayerItem[]) {
        super(mediator)
        for (let index = 0; index < settings.length; index++) {
            const element = settings[index];
            let mgr: LayerManager = new LayerManager()
            mgr.init(element, root)
            this.managers[index] = mgr;
        }
    }

    isValid() {
        return true;
    }

    init(): void {
        this._mediator.subscribe(new AppEvent(AppEventName.app_pushView, this.pushView, this))
        this._mediator.subscribe(new AppEvent(AppEventName.app_removeView, this.removeView, this))
        this._mediator.subscribe(new AppEvent(AppEventName.app_popView, this.popView, this))
    }
    private root: any;
    getRoot() {
        return this.root;
    }

    /**
     * 隐藏底层UI,减少显示对象个数
     * @param layerIndex 
     * @param flag 
     */
    setVisible(layerIndex: number, flag: boolean) {
        this.managers[layerIndex].setVisible(flag)
    }

    removeView(layer: IUIView) {
        let layerIndex = layer.getLayerIndex()
        let manager = this.managers[layerIndex];
        if (manager) {
            manager.removeView(layer)
        }
    }
    /**
      * 此方法用于关闭界面，为了兼容Android的back键 所以layer有为null的情况。
      * 如果 返回false 表明已经没有界面可以弹出
      * @param layer 
      */
    popView(layer?: IUIView): IUIView {
        // console.log('popView layer is ', layer)
        if (layer) {
            let index: number = layer.getLayerIndex()
            // console.log(' popView  index is ', index, ' layer is ', layer)
            let manger = this.managers[index];
            if (!manger) {
                // console.log(' popView layer is not found type is ', type)
                return null;
            } else {
                return manger.popView(layer);

            }

        } else {
            for (let index = this.managers.length - 1; index >= 0; index--) {
                const manager = this.managers[index];
                if (manager.canPop() && manager.count() > 0) {
                    let view = manager.popView(null)
                    if (view) {
                        return view;
                    }
                }
            }
            return null;
        }

    }
    /**
     * 清除UI
     */
    clear() {
        for (let index = this.managers.length - 1; index >= 0; index--) {
            const manager = this.managers[index];
            manager.clear()
        }
    }

    /**
     * 用于在启动页的时候直接将节点放入管理器。实际场景中
     * 已经加载了该节点。
     * @param layer 
     * @param layerIndex 
     */
    addView(layer: IUIView) {
        if (layer) {
            let layerIndex: number = layer.getLayerIndex()
            let manager = this.managers[layerIndex];
            if (!manager) {
                console.log(' manager is null ', layer)
                return;
            }
            if (manager.isValid()) {
                manager.addView(layer);
            }
        }
    }

    //添加UI
    pushView(layer: IUIView) {
        if (layer) {
            let layerIndex = layer.getLayerIndex()
            let manager = this.managers[layerIndex];
            if (!manager) {
                console.log(' manager is null ', layer)
                return;
            }
            if (manager.isValid()) {
                manager.pushView(layer);
            }

        }
    }


    removeByPath(path: string) {

        for (let index = 0; index < this.managers.length; index++) {
            const element = this.managers[index];
            if (element.removeByPath(path)) {
                break;
            }
        }
    }

}